# 1. 添加cmake版本说明
cmake_minimum_required(VERSION 3.1.3)
# 2. 声明工程名称
project(message_server)
set(target "message_server") 
set(test_client "message_client")
# 3. 检测并生成proto框架代码
#   a. 添加所需的proto映射代码文件名称
set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
set(proto_files file.proto base.proto message.proto user.proto)
#   b. 检测框架代码文件是否已经生成
set(proto_h "")
set(proto_cc "")
set(proto_srcs "")
foreach(proto_file ${proto_files})
#   c. 如果没有生成，则预定义生成指令 -- 用于在构建项目之间先生成框架代码
    string(REPLACE ".proto" ".pb.h" proto_h ${proto_file})
    string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file})
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${proto_cc})
        add_custom_command(
            PRE_BUILD
            COMMAND protoc
            ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I ${proto_path} --experimental_allow_proto3_optional ${proto_path}/${proto_file}
            DEPENDS ${proto_path}/${proto_file}
            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
            COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
        )
    endif()
#   d. 将所有生成的框架源码文件名称保存起来
    list(APPEND proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})
endforeach()
# 4. 检测并生成ODB框架代码
#   a. 添加所需的ODB映射代码文件名称
set(odb_path ${CMAKE_CURRENT_SOURCE_DIR}/../odb)
set(odb_files message.hxx)
#   b. 检测框架代码文件是否已经生成
set(odb_hxx "")
set(odb_cxx "")
set(odb_srcs "")
foreach(odb_file ${odb_files})
#   c. 如果没有生成，则预定义生成指令 -- 用于在构建项目之间先生成框架代码
    string(REPLACE ".hxx" "-odb.hxx" odb_hxx ${odb_file})
    string(REPLACE ".hxx" "-odb.cxx" odb_cxx ${odb_file})
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${odb_cxx})
        add_custom_command(
            PRE_BUILD
            COMMAND odb
            ARGS -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time ${odb_path}/${odb_file}
            DEPENDS ${odb_path}/${odb_file}
            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
            COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
        )
    endif()
#   d. 将所有生成的框架源码文件名称保存起来
    list(APPEND odb_srcs ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx})
endforeach()

# 5.获取源码目录下的所有源码文件
set(src_server_files "")
set(src_client_files "")
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_server_files)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/test src_client_files)

link_directories(/usr/lib)

# 6. 声明目标及依赖
add_executable(${target} ${src_server_files} ${proto_srcs} ${odb_srcs})
add_executable(${test_client} ${src_client_files} ${proto_srcs} ${odb_srcs}) 

# 7. 设置头文件默认搜索路径
include_directories(/usr/include/mysql)
include_directories(/usr/include/jsoncpp/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../Common)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third/include)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# 8. 设置需要链接的库

target_link_libraries(${target}  -lgflags -lgtest -pthread 
-lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf 
-lleveldb -letcd-cpp-api -lcpprest -lcurl
-lodb-mysql  -lodb -lodb-boost /usr/lib/libamqpcpp.so -lev
-ljsoncpp -lcpr -lelasticlient)

target_link_libraries(${test_client}  -lgflags  -lgtest -pthread
-lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf 
-lleveldb -letcd-cpp-api -lcpprest -lcurl
-lodb-mysql  -lodb -lodb-boost 
-lamqpcpp -lev -ljsoncpp -lcpr -lelasticlient)

# 9. 设置安装路径
INSTALL(TARGETS ${target} ${test_client} RUNTIME DESTINATION bin)
